import { app } from "../app";
import { getKlinesByCode } from "../dao/kline";
import { updateKlinesByQtClist } from "../schedule";
import { saveAllKlinesByEastMoney, saveKlinesByEastMoney } from "../utils/kline";


const klineRouter = () => {
    app.post("/kline/save", async (req, res: any) => {
        const { secid } = req.body;
        const success = await saveKlinesByEastMoney({ secid });
        return res.json({
            code: success ? 200 : 500,
            message: success ? "保存成功" : "保存失败",
        });
    });

    app.post("/kline/save/all", async (req, res: any) => {
        await saveAllKlinesByEastMoney();
        return res.json({
            code: 200,
            message: "所有K线数据更新完毕",
        });
    });

    app.get("/kline/get/:code", async (req, res: any) => {
        const { code } = req.params;
        const klines = await getKlinesByCode(code);
        return res.json({
            code: 200,
            message: "获取成功",
            data: klines,
        });
    });

    app.post("/kline/update/qt/clist", async (req, res: any) => {
        await updateKlinesByQtClist();
        return res.json({
            code: 200,
            message: "所有K线数据更新完毕",
        });
    });
};

export default klineRouter;